﻿using BLL;
using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms
{
    public partial class FrmCheckAttendance : Form
    {
        public FrmCheckAttendance()
        {
            InitializeComponent();
            #region   初始化控件缩放

            x = Width;
            y = Height;
            setTag(this);

            #endregion
        }

        private void FrmCheckAttendance_Load(object sender, EventArgs e)
        {
            ActivityManage activitybll = new ActivityManage();
            dgvActlist.AutoGenerateColumns = false;
            dgvActlist.DataSource = activitybll.GetALLActivitiesByClubID(FrmLogin.clubid);

            MemberManage memberbll = new MemberManage();
            dgvMember.AutoGenerateColumns = false;
            dgvMember.DataSource = memberbll.GetAllMember(FrmLogin.clubid);



            DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
            buttonColumn.HeaderText = "按钮列";
            buttonColumn.DefaultCellStyle.NullValue = "查看";
            this.dgvMember.Columns.Add(buttonColumn);


            Activity act = (Activity)dgvActlist.CurrentRow.DataBoundItem;
            lblActivity.Text = act.Activityname;
            lblDate.Text = act.Activitydate.ToShortDateString();

        }


        int currentactivityid = 0;
        private void dgvActList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Activity act = (Activity)dgvActlist.CurrentRow.DataBoundItem;
            lblActivity.Text = act.Activityname;
            lblDate.Text = act.Activitydate.ToShortDateString();
            currentactivityid = act.Activityid;
            btnSave.Enabled = true;
            for (int i = 0; i < dgvMember.Rows.Count; i++)
            {
                dgvMember.Rows[i].Cells[0].Value = false;
            }
            for (int i = 0; i < dgvMember.Rows.Count; i++)
            {
                AttendanceManage attendancebll = new AttendanceManage();
                string memberid = dgvMember.Rows[i].Cells[1].Value.ToString();
                Attendance attendance = attendancebll.GetAttendance(memberid, currentactivityid);
                if (attendance != null) { dgvMember.Rows[i].Cells[0].Value = attendance.Attend; }
            }

        }

        private void dgvMember_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Activity cm = (Activity)dgvActlist.CurrentRow.DataBoundItem;
            //currentactivityid = cm.Activityid;
            lblActivity.Text = cm.Activityname;
            lblDate.Text=cm.Activitydate.ToShortDateString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            AttendanceManage attendancebll = new AttendanceManage();
            int i;
            for (i = 0; i < dgvMember.Rows.Count; i++)
            {
                bool attend = Convert.ToBoolean(dgvMember.Rows[i].Cells[0].Value);
                string memberid = dgvMember.Rows[i].Cells[1].Value.ToString();
                Attendance attendance = attendancebll.GetAttendance(memberid, currentactivityid);
                if (attendance == null) 
                {
                    attendance = new Attendance(0, currentactivityid, memberid, attend);
                    attendancebll.AddAttendance(attendance);
                }
                else 
                {
                    attendance = new Attendance(attendance.Id, currentactivityid, memberid, attend);
                    attendancebll.UpdateAttendance(attendance);
                }
            }
            if (i == dgvMember.Rows.Count) 
            {
                MessageBox.Show("保存成功", "系统消息", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }
        }

        private void dgvMember_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvMember.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex > -1)
            {
                MessageBox.Show("您查看的是成员" + dgvMember.Rows[e.RowIndex].Cells[2].Value + "的考勤记录！", "系统消息");
            }
        }

        private void chkCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCheckAll.Checked)
            {
                for (int i = 0; i < dgvMember.Rows.Count; i++)
                {
                    dgvMember.Rows[i].Cells[0].Value = true;
                }
            }
            else 
            {
                for (int i = 0; i < dgvMember.Rows.Count; i++)
                {
                    dgvMember.Rows[i].Cells[0].Value = false;
                }
            }
            
        }

        private void FrmCheckAttendance_Resize(object sender, EventArgs e)
        {
            //重置窗口布局
            ReWinformLayout();
        }

        #region 控件大小随窗体大小等比例缩放

        private readonly float x; //定义当前窗体的宽度
        private readonly float y; //定义当前窗体的高度

        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0) setTag(con);
            }
        }

        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    var mytag = con.Tag.ToString().Split(';');
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx); //宽度
                    con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy); //高度
                    con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx); //左边距
                    con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy); //顶边距
                    var currentSize = Convert.ToSingle(mytag[4]) * newy; //字体大小                   
                    if (currentSize > 0) con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    con.Focus();
                    if (con.Controls.Count > 0) setControls(newx, newy, con);
                }
        }


        /// <summary>
        /// 重置窗体布局
        /// </summary>
        private void ReWinformLayout()
        {
            var newx = Width / x;
            var newy = Height / y;
            setControls(newx, newy, this);

        }

        #endregion
    }
}
